package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ThreadLocalUtil;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //条件查询
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);
        //通过PageBean的of方法快速构建一个对象
        return PageBean.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.copyProperties(nursingProject, NursingProjectVo.class);
    }

    /**
     * 编辑护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用禁用护理项目
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisableNursingProject(Long id, Integer status) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllNursingProjects() {
        List<NursingProject> nursingProjects = nursingProjectMapper.selectAll();
        return BeanUtil.copyToList(nursingProjects, NursingProjectVo.class);
    }
}
